package services

import (
	"awesome-devops/applications/models"
	"awesome-devops/boot"
	"fmt"
	"gitee.com/jjawesomejj/awesome-util/helper/commonhelper"
)

type PublishService struct {
}

func (service *PublishService) DeployConfig(projectId int) interface{} {
	process := models.Process{}
	query := boot.GetDbConnection().Table(process.TableName())
	var list []map[string]interface{}
	result := query.Select("*").Where("delete_time=0 and project_id=?", projectId).Find(&list)
	if result.Error != nil {
		return result.Error
	}
	configIds := []int{}
	stepJsons := make([]int, 0)
	fileJsonIds := make([]int, 0)
	for _, item := range list {
		configIds = append(configIds, int(item["id"].(uint32)))
		if item["before_upload_step_json"] != nil {
			var stepJson []int
			err := commonhelper.JsonDecodeWithType(item["before_upload_step_json"].(string), &stepJson)
			if err == nil && stepJson != nil {
				stepJsons = append(stepJsons, stepJson...)
			}
		}
		if item["files"] != nil {
			var fileJson []int
			err := commonhelper.JsonDecodeWithType(item["files"].(string), &fileJson)
			if err == nil && fileJson != nil {
				fileJsonIds = append(fileJson, fileJsonIds...)
			}
		}
		if item["after_upload_step_json"] != nil {
			var stepJson []int
			err := commonhelper.JsonDecodeWithType(item["after_upload_step_json"].(string), &stepJson)
			if err == nil && stepJson != nil {
				stepJsons = append(stepJsons, stepJson...)
			}
		}
	}
	var stepDatas []models.RunStep
	step := models.RunStep{}
	stepDatasInterface, err := step.RowsByIDs(stepDatas, step.TableName(), stepJsons)
	stepDatas = stepDatasInterface.([]models.RunStep)
	if err != nil {
		return nil
	}
	stepMap := map[uint]models.RunStep{}
	for _, item := range stepDatas {
		stepMap[item.ID] = item
	}
	fileModel := models.ProcessFiles{}
	var fileList []models.ProcessFiles
	fileListInteface, err := fileModel.RowsByIDs(fileList, fileModel.TableName(), fileJsonIds)
	if err != nil {
		return err
	}
	fileList = fileListInteface.([]models.ProcessFiles)

	fileJsonMap := map[uint]models.ProcessFiles{}
	for _, value := range fileList {
		fileJsonMap[value.Id] = value
	}
	fmt.Println(fileJsonMap, "res==>")
	for index, item := range list {
		if item["files"] != nil {
			var filesJson []int
			err := commonhelper.JsonDecodeWithType(item["files"].(string), &filesJson)
			files := make([]models.ProcessFiles, 0)
			if err != nil {
				list[index]["files"] = files
			} else {
				for _, fileId := range filesJson {
					if value, ok := fileJsonMap[uint(fileId)]; ok {
						files = append(files, value)
					}
				}
			}
			list[index]["files"] = files
		}

		if item["server_id_json"] != nil {
			var stepListConfig []int
			err := commonhelper.JsonDecodeWithType(item["server_id_json"].(string), &stepListConfig)
			if err == nil {
				list[index]["server_id_json"] = stepListConfig
			}
		}
		if item["before_upload_step_json"] != nil {
			var stepJson []int
			err := commonhelper.JsonDecodeWithType(item["before_upload_step_json"].(string), &stepJson)
			if err == nil && stepJson != nil {
				datas := make([]models.RunStep, 0)
				for _, id := range stepJson {
					if stepItem, ok := stepMap[uint(id)]; ok {
						datas = append(datas, stepItem)
					}
				}
				list[index]["before_upload_step_json"] = datas
			}
		}
		if item["after_upload_step_json"] != nil {
			var stepJson []int
			err := commonhelper.JsonDecodeWithType(item["after_upload_step_json"].(string), &stepJson)
			if err == nil && stepJson != nil {
				datas := make([]models.RunStep, 0)
				for _, id := range stepJson {
					if stepItem, ok := stepMap[uint(id)]; ok {
						datas = append(datas, stepItem)
					}
				}
				list[index]["after_upload_step_json"] = datas
			}
		}
	}
	if list == nil {
		return make([]interface{}, 0)
	}
	return list
}
